<?php
require_once 'Router.php';
require_once 'BootStrap.php';

class Dispatcher {
	protected $c;
	protected $a;
	protected $params;
	protected $object;
	
	public function __construct() {
		if( !isset($_REQUEST['c']) || !isset($_REQUEST['a']) ) {
			$this->c = "index";
			$this->a = "index";
		} else {
			$this->c = $_REQUEST['c'];
			$this->a = $_REQUEST['a'];
		}
		$this->params = $_REQUEST;
		
		BootStrap($this->params);			// 预处理
		$this->Route($this->c, $this->a, $this->params);
	}

	// 路由
	public function Route($controller, $action, $params) {
		global $controllerMap;
		global $actionMap;
		
		$router = new Router($controller);
		
		// 调用ACTION
		$result = $router->call($action, $params);
		$this->object = $router->object;
		
		$redirect = $this->object->getRedirect();
		if( $redirect ) {		// 控制器跳转
			$this->redirect($redirect["controller"], $redirect["action"], $redirect["params"]);
		}
		
	}
	
	public function redirect($controller, $action, $params) {
		$this->Route($controller, $action, $params);
	}
}